<template>
  <div v-if="form.carInsuranceSource ==1">
    <div class="card-wrap flex-column justify-end card-padding">
      <el-form :model="form" ref="registrationInformation" label-width="140px" :rules="type =='detail'?{}:rules" :label-position="type =='detail'?'right':'top'">
        <div class="card-title-wrap">
          <span>上牌信息</span>
          <span></span>
        </div>
        <div style="box-sizing: border-box;padding-left: 20px;">
          <el-row>
            <el-col :span="8">
              <el-form-item label="车牌号:" prop="carNo" style="width: 80%;">
                <el-input v-if="type !=='detail'" v-model="form.carNo" maxlength="30"></el-input>
                <span v-if="type=='detail'">{{form.carNo}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="上牌时间:" prop="registrationDate">
                <el-date-picker v-if="type !=='detail'" v-model="form.registrationDate" type="date" placeholder="选择日期" :style="{width: type=='detail'?'70%':'80%'}" />
                <span v-if="type=='detail'">{{form.registrationDate}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="是否限牌城市:" prop="restrictedCityFlag">
                <el-select v-if="type !=='detail'" v-model="form.restrictedCityFlag" clearable :style="{width: type=='detail'?'70%':'80%'}">
                  <template v-for="dict in dict.type.business_yes_no">
                    <el-option :label="dict.label" :value="dict.value" :key="dict.value"></el-option>
                  </template>
                </el-select>
                <dict-tag v-if="type =='detail'" :options="dict.type.business_yes_no" :value="form.restrictedCityFlag" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item label="车辆登记证附件:" prop="registrationFile">
                <ImageCommon :up-type="2" info="选择附件" :limit="1" v-model="form.registrationFile" :disabled="type=='detail'"></ImageCommon>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col>
              <el-form-item label="行驶本附件:" prop="driverFile">
                <ImageCommon @input="imageChange" :up-type="2" info="选择附件" :limit="1" v-model="form.driverFile" :disabled="type=='detail'"></ImageCommon>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>

    </div>

  </div>
</template>
  <script>
import { uploadDriverLicense } from "@/api/cwgd/registrationApi.js";
export default {
  name: "registrationInformation",
  props: {
    value: {
      type: Object,
      default: {},
    },
    state: {
      type: String,
      default: "",
    },
  },
  dicts: ["business_yes_no"],

  computed: {
    form: {
      get() {
        let that = this;
        const proxy = new Proxy(that.value, {
          get(target, key) {
            return Reflect.get(target, key);
          },
          set(target, key, value) {
            that.$emit("input", { ...target, [key]: value });
            return true;
          },
        });
        return proxy;
      },
    },
    type() {
      let status;
      switch (this.state) {
        case "write":
          status = "edit";
          break;
        case "disabled":
          status = "detail";
          break;
        case "readonly":
          status = "detail";
          break;
        case "detail":
          status = "detail";
          break;
        case "edit":
          status = "edit";
        case "add":
          status = "add";
          break;
      }
      return status;
    },
  },
  data() {
    return {
      buttonLoading: false,
      rules: {
        carNo: [{ required: true, message: "请输入车牌号", trigger: "blur" }],
        // carVin: [{ required: true, message: "请输入车架号", trigger: "blur" }],
        // engineNo: [
        //   { required: true, message: "请输入发动机号码", trigger: "blur" },
        // ],
        registrationDate: [{ required: true, message: "请选择上牌时间", trigger: "blur" }],
        restrictedCityFlag: [{ required: true, message: "请选择是否限牌城市", trigger: "change" }],
        registrationFile: [{ required: true, message: "请上传附件", trigger: "blur" }],
        driverFile: [{ required: true, message: "请上传附件", trigger: "blur" }],
      },
    };
  },
  methods: {
    imageChange(url) {
      let that = this;
      if (url) {
        console.log(url, '==');
        this.buttonLoading = true;
        uploadDriverLicense(url)
          .then(res => {
            if (res.code == '200') {
              this.$set(that.form, 'carNo', res.data.licensePlateNum)
            } else {

            }
          })
          .catch(e => {
          })
          .finally(f => {
            this.buttonLoading = false;
          });
      }
    },
    valid() {
      return new Promise(async (resolve) => {
        let result = true;
        let refs = Object.values(this.$refs);
        for (let index = 0; index < refs?.length; index++) {
          const ref = refs[index];
          if (Object.prototype.toString.call(ref) == "[object Array]") {
            for (let i = 0; i < ref.length; i++) {
              const element = ref[i];
              if (element?.valid) {
                let rc = await element?.valid?.();
                result = result && rc;
              }
              if (element?.validate) {
                let rf = await this._valid(ref);
                result = result && rf;
              }
              if (element?.form_validate) {
                let rf = await this._valid(ref, "form_validate");
                result = result && rf;
              }
            }
          } else {
            if (ref?.valid) {
              let rc = await ref?.valid?.();
              result = result && rc;
            }
            if (ref?.validate) {
              let rf = await this._valid(ref);
              result = result && rf;
            }
            if (ref?.form_validate) {
              let rf = await this._valid(ref, "form_validate");
              result = result && rf;
            }
          }
        }
        resolve(result);
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.box {
  width: 100%;
  .title {
    border-left: 3px solid #3280fc;
    padding-left: 5px;
    box-sizing: border-box;
  }
}
.app-container {
  background-color: #fff;
}
</style>
